package rkw.database.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import core.database.AbstractEntity;

@Entity
@Table(name = "administratorToken")
public class AdministratorToken extends AbstractEntity {
	@Column(name = "value", unique = true, length = 36)
	private String value;

	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
	@JoinColumn(name = "election")
	private Election election;

	@ManyToOne(cascade = { CascadeType.DETACH, CascadeType.PERSIST, CascadeType.MERGE })
	@JoinColumn(name = "member")
	private Member member;

	public AdministratorToken() {
		super();
	}

	public AdministratorToken(Member member, Election election) {
		super();
		this.election = election;
		election.getAdministratorTokens().add(this);
		this.member = member;
		member.getTokens().add(this);
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public Election getElection() {
		return election;
	}

	public void setElection(Election election) {
		this.election = election;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public void removeFromElection() {
		this.member.getTokens().remove(this);
		this.election.getAdministratorTokens().remove(this);
	}
}
